package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    
    @Autowired
    private CategoryBrandService categoryBrandService;
    
    @Override
    public List<BrandDTO> queryBrandByCategoryId(Long categoryId) {
        //根据分类id查询中间表
        List<CategoryBrand> list = categoryBrandService.query().eq("category_id", categoryId).list();
        //处理数据，得到品牌Id集合
        List<Long> collect = list.stream().map(CategoryBrand::getBrandId).collect(Collectors.toList());
        //查询集合
        List<Brand> brands = listByIds(collect);
        //处理结果并返回
        return brands.stream().map(BrandDTO::new).collect(Collectors.toList());
    }

    @Override
    public PageDTO<BrandDTO> queryByPage(String key, Integer page, Integer rows) {
        if(page<= 0 || rows <= 0 ){
            throw new LyException(400,"页码超出正常范围！");
        }
        //判断是否有搜索条件
        boolean isnotBlank = StringUtils.isNotBlank(key);
        //查询
        Page<Brand> result = query().
                like(isnotBlank, "`name`", key).or().eq(isnotBlank, "letter", key)
                .page(new Page<>(page, rows));
        //封装，转换结果
        List<Brand> records = result.getRecords();
        List<BrandDTO> dtoList = records.stream().map(BrandDTO::new).collect(Collectors.toList());
        //返回结果
        return new PageDTO<>(result.getTotal(),result.getPages(),dtoList);

    }

    @Override
    @Transactional //添加事务注解
    public void saveBrand(BrandDTO brandDTO) {
        //1  新增
        //1.1 转po
        Brand brand = brandDTO.toEntity(Brand.class);
        //1.2 新增操作
        boolean isSuccess = save(brand);
        //1.3 判断
        if(!isSuccess){
            throw new LyException(500,"品牌新增失败！");
        }
        //1.4 回显Id
        Long brandId = brand.getId();
        //2 新增中间表
        //2.1 获取分类id
        List<Long> categoryIds = brandDTO.getCategoryIds();
        //2.2封装中间表对象
        List<CategoryBrand> cbList = new ArrayList<>(categoryIds.size());
        for (Long categoryId : categoryIds) {
           /* CategoryBrand cb = new CategoryBrand();
            cb.setBrandId(brandId);
            cb.setCategoryId(categoryId);
            cbList.add(cb);*/

           cbList.add(CategoryBrand.of(brandId,categoryId));

        }

        //2.3 批量新增中间表
        categoryBrandService.saveBatch(cbList);
    }

}
